// pages/register/register.js
var Bmob = require("../../utils/Bmob-2.2.0.min.js")
Bmob.initialize("a25cd3ee1411fbed", "999423");

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },


  submit: function(e) {
    console.log(e.detail.value)
    let params = {
      username: e.detail.value.usename,
      password: e.detail.value.usepass,
      // email: 'bmob2018@bmob.cn',
      // phone: '13711166567',
    }
    Bmob.User.register(params).then(res => {
      console.log(res)
      Bmob.User.login(params.username, params.password).then(res => {
        console.log(res)
        var status = true;
        var usedata = {
          useId: res.objectId,
          useName: res.username,
          status: status
        }
        wx.setStorageSync("usedata", usedata)
        console.log("sdasd")
        wx.switchTab({
          url: '../homepage/homepage',
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      }).catch(err => {
        console.log(err)
      });


    }).catch(err => {
      console.log(err)
      Bmob.User.login(params.username, params.password).then(res => {
        console.log(res)
        var status = true;
        var usedata = {
          useId: res.objectId,
          useName: res.username,
          status: status
        }
        wx.setStorageSync("usedata", usedata)
        wx.switchTab({
          url: '../homepage/homepage',
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
        
      }).catch(err => {
        console.log(err)
        wx.showToast({
          title: '密码错误',
          icon: 'loading',
          duration: 2000
        })
      });

    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})